<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 14/01/2019
 * Time: 17:40
 */

namespace App\Services\Cargo;


use App\Services\Cargo\Cargo\CargoLogic;

class Cargo
{
    /**
     * 发布货物
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function addCargo($data)
    {
        ValidateCargo::checkValidate($data, [
            'start_addr',
            'end_addr',
            'receiver_name',
            'receiver_phone',
            'type',
            'weight',
            'volume',
            'car_lenght',
            'car_type',
            'price',
            'remark',
            'loading_time',
            'user_id',
            'is_common',
            'start_province_id',
            'start_city_id',
            'start_county_id',
            'end_province_id',
            'end_city_id',
            'end_county_id',
            'create_ip',
            'pay_pass',
            'deposit',
            'pay_type'
        ]);
        return (new CargoLogic())->addCargo($data);
    }

    /**
     * 修改货物信息
     * @param $data
     * @throws \App\Exceptions\RequestFailException
     */
    public function changeComCargo($data)
    {
        $data = pick_array($data, ['user_id', 'id', 'addr', 'name', 'phone', 'province_id', 'city_id', 'county_id']);
        ValidateCargo::checkValidate($data, ['user_id', 'id', 'addr', 'name', 'phone', 'province_id', 'city_id', 'county_id']);
        return (new CargoLogic())->changeComCargo($data);
    }

    /**
     * 获取我的货源列表
     * @param $data
     * @return array
     */
    public function getCargoList($data)
    {
        $data = pick_array($data, ['user_id', 'page', 'status']);
        ValidateCargo::checkValidate($data, ['page', 'user_id', 'status']);
        return (new CargoLogic())->getCargoList($data);
    }

    /**
     * 添加常发货源
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function addCommonCargo($data)
    {
        $data = pick_array($data, ['user_id', 'addr', 'name', 'phone', 'province_id', 'city_id', 'county_id']);
        ValidateCargo::checkValidate($data, ['user_id', 'addr', 'name', 'phone', 'province_id', 'city_id', 'county_id']);
        return (new CargoLogic())->addCommonCargo($data);
    }

    /**
     * 获取常发货源列表
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function getComCargoList($data)
    {
        $data = pick_array($data, ['page', 'user_id']);
        ValidateCargo::checkValidate($data, ['page', 'user_id']);
        return (new CargoLogic())->getComCargoList($data);
    }

    /**
     * 根据id删除常发货源
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function delComCargo($data)
    {
        $data = pick_array($data, ['id']);
        ValidateCargo::checkValidate($data, ['id']);
        return (new CargoLogic())->delComCargo($data);
    }

    /**
     * 车主查找货物
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function findCargos($data)
    {
        $data = pick_array($data, [
            'page',
            'start_province_id',
            'start_city_id',
            'start_county_id',
            'end_province_id',
            'end_city_id',
            'end_county_id',
            'car_type',
            'car_length'
        ]);
        ValidateCargo::checkValidate($data, ['page']);
        return (new CargoLogic())->findCargos($data);
    }

    /**
     * 获取货源列表
     */
    public function backCargoList($data)
    {
        $data = init_array($data, ['keywords']);
        return (new CargoLogic())->backCargoList($data);
    }

    /**
     * 根据id获取车辆详情
     * @param $data
     */
    public function getCargoDetail($data)
    {
        $data = pick_array($data, ['id', 'user_id']);
        ValidateCargo::checkValidate($data, ['id', 'user_id']);
        return (new CargoLogic())->getCargoDetail($data);
    }
}
